
package com.lisn.fragmentationupload.upload;

import android.util.Log;

import com.lisn.fragmentationupload.utils.MLog;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * 上传管理器
 */
public class UploadManager {

    private static final String TAG = UploadManager.class.getSimpleName();

    private OkHttpClient mClient;

    private int mPoolSize = 20;
    // 将执行结果保存在future变量中
    private Map<String, Future> mFutureMap;
    private ExecutorService mExecutor;
    private Map<String, UploadTask> mCurrentTaskList;

    static UploadManager manager;

    /**
     * 方法加锁，防止多线程操作时出现多个实例
     */
    private static synchronized void init() {
        if (manager == null) {
            manager = new UploadManager();
        }
    }

    /**
     * 获得当前对象实例
     *
     * @return 当前实例对象
     */
    public final static UploadManager getInstance() {
        if (manager == null) {
            init();
        }
        return manager;
    }

    public UploadManager() {
        initOkhttpClient();
        // 初始化线程池
        mExecutor = Executors.newFixedThreadPool(mPoolSize);
        mFutureMap = new HashMap<>();
        mCurrentTaskList = new HashMap<>();
    }

    /**
     * 初始化okhttp
     */
    private void initOkhttpClient() {
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.connectTimeout(10, TimeUnit.SECONDS);
        okBuilder.readTimeout(10, TimeUnit.SECONDS);
        okBuilder.writeTimeout(10, TimeUnit.SECONDS);
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                try {
                    if (message.length() > 500) {
                        // Log.e("OKHttp-----", message);
                    } else {
                        String text = URLDecoder.decode(message, "utf-8");
                        Log.e("OKHttp-----", text);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    Log.e("OKHttp-----", message);
                }
            }
        });
        //这行必须加 不然默认不打印
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okBuilder.addInterceptor(interceptor);
        mClient = okBuilder.build();
    }

    /**
     * 添加上传任务
     *
     * @param uploadTask
     */
    public void addUploadTask(UploadTask uploadTask) {
        if (uploadTask != null && !isUploading(uploadTask)) {
            uploadTask.setClient(mClient);
            uploadTask.setUploadStatus(UploadStatus.UPLOAD_STATUS_INIT);
            // 保存上传task列表
            mCurrentTaskList.put(uploadTask.getId(), uploadTask);
            Future future = mExecutor.submit(uploadTask);
            MLog.e(TAG, "addUploadTask: " + future);
            mFutureMap.put(uploadTask.getId(), future);
        }
    }

    private boolean isUploading(UploadTask task) {
        if (task != null && task.getUploadStatus() == UploadStatus.UPLOAD_STATUS_UPLOADING) {
            return true;
        }
        return false;
    }

    /**
     * 暂停上传任务
     *
     * @param id 任务id
     */
    public void pause(String id) {
        UploadTask task = getUploadTask(id);
        if (task != null) {
            task.setUploadStatus(UploadStatus.UPLOAD_STATUS_PAUSE);
        }
    }

    /**
     * 重新开始已经暂停的上传任务
     *
     * @param id 任务id
     */
    public void resume(String id) {
        UploadTask task = getUploadTask(id);
        if (task != null) {
            addUploadTask(task);
        }
    }


    /**
     * 实时更新manager中的task信息
     *
     * @param task
     */
    public void updateUploadTask(UploadTask task) {
        if (task != null) {
            UploadTask currTask = getUploadTask(task.getId());
            if (currTask != null) {
                mCurrentTaskList.put(task.getId(), task);
            }
        }
    }

    /**
     * 获得指定的task
     *
     * @param id task id
     * @return
     */
    public UploadTask getUploadTask(String id) {
        UploadTask currTask = mCurrentTaskList.get(id);
        if (currTask == null) {
            currTask = parseEntity2Task(new UploadTask.Builder().build());
            // 放入task list中
            mCurrentTaskList.put(id, currTask);
        }
        return currTask;
    }


    private UploadTask parseEntity2Task(UploadTask currTask) {
        UploadTask.Builder builder = new UploadTask.Builder()
                .setUploadStatus(currTask.getUploadStatus())
                .setFileName(currTask.getFileName())
                .setFilePath(currTask.getFilePath())
                .setUrl(currTask.getUrl())
                .setId(currTask.getId());
        currTask.setBuilder(builder);
        return currTask;
    }
}
